package cn.kgc.controller;

import cn.kgc.model.PageWrapper;
import cn.kgc.model.User;
import cn.kgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Rock
 * @create 2020-08-25 17:06
 */
@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(Model model) {
        User user6 = userService.selectUserById(6);
        User user7 = userService.selectUserById(7);
        model.addAttribute("user6", user6);
        model.addAttribute("user7", user7);
        return "/index";
    }

    @RequestMapping("/tm")
    public String tm() {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("id", 6);
        map1.put("money", -1000.0);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", 7);
        map2.put("money", +1000.0);
        userService.updateUserMoney(map1, map2);
        return "redirect:index";
    }

    @RequestMapping("/selectUserList")
    @ResponseBody
    public List<User> selectUserList() {
        return userService.selectUserList();
    }

    @GetMapping("/selectUserList/{page}/{size}")
    @ResponseBody
    public PageWrapper selectUserList(@PathVariable Integer page, @PathVariable Integer size) {
        return userService.selectUserList(page, size);
    }

    /** RESTful 只进行演示 没有做数据持久化 同时支持Jsp 客户端(android ios...) **/

    @PostMapping("/insert")
    @ResponseBody
    public Map insert(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        return map;
    }

    @PostMapping("/insertX")
    @ResponseBody
    public Map insertX(HttpServletRequest request, String name, MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("path", saveFile(request, file));
        return map;
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Map delete(@PathVariable Integer id, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        return map;
    }

    @PutMapping("/update/{id}")
    @ResponseBody
    public Map update(@PathVariable Integer id, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        return map;
    }

    @PutMapping("/updateX/{id}")
    @ResponseBody
    public Map updateX(@PathVariable Integer id, HttpServletRequest request, String name, MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("path", saveFile(request, file));
        return map;
    }

    @GetMapping("/select/{id}")
    @ResponseBody
    public Map select(@PathVariable Integer id, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        return map;
    }

    private String saveFile(HttpServletRequest request, MultipartFile file) throws Exception {
        if (null != file && !file.isEmpty()) {
            String fileName = UUID.randomUUID().toString().replace("-", ""); //文件名
            String originalFilename = file.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String path = request.getServletContext().getRealPath("/upload") + "/" + fileName + substring;
            file.transferTo(new File(path));
            return path;
        }
        return "";
    }
}
